%% Copyright (C) 2007 Vijay Gajula <gajulak@gmail.com>.
%% All rights reserved.
%%
%% Redistribution and use in source and binary forms, with or without
%% modification, are permitted provided that the following conditions
%% are met:
%%
%% 1. Redistributions of source code must retain the above copyright
%%    notice, this list of conditions and the following disclaimer.
%% 2. Redistributions in binary form must reproduce the above
%%    copyright notice, this list of conditions and the following
%%    disclaimer in the documentation and/or other materials provided
%%    with the distribution.
%%
%% THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
%% OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
%% WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
%% ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
%% DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
%% DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
%% GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
%% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
%% WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
%% NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
%% SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

-module(mod_interface).
-vsn(1.0).
-author('gajulak@gmail.com').
-export([process_get/1, process_edit/1, sys_interface/1]).
-export([interface_info/1]).

-include("xmerl.hrl").
-include("netconf.hrl").

-define(TREE, { name,
		[ ipaddress,
		  netmask,
		  type,
		  mtu,
		  status ]
		}).



process_get(E) ->
	%% io:format("mod_interface:~p~n",[E]),
	sys_interface(E).

sys_interface([#xmlElement{name = all}=E|T]) 
   when record(E, xmlElement) ->
	interface_info(all);
	
sys_interface([H|T]) -> sys_interface(T);
sys_interface([]) -> ok.

interface_info(all) ->
	Interface = db:get_interface(),
	Xml = generate_xml(Interface, []),
	{ok, Xml}.

generate_xml([], Xml) -> Xml;
generate_xml([H|T], Xml) ->
     Str = io_lib:format("
			<interface>
			<name>~s</name>
			<ipaddress>~s</ipaddress>
			<netmask>~s</netmask>
			<type>~p</type>
			<mtu>~p</mtu>
			<status>~p</status>
			</interface>~n", 
			[H#interface.name, H#interface.ipaddress,
			 H#interface.netmask, H#interface.type,
			 H#interface.mtu, H#interface.status]),
      generate_xml(T, Xml++Str).


process_edit(E) ->
	%% io:format("mod_interface:~p~n",[E]),
	R = top_node(?TREE, E, #interface{}),
	%% io:format("End process_edit:~p~n",[R]),
	R.

top_node({Node, SubNode}, [#xmlElement{name = Node}=E|T], If) 
   when record(E, xmlElement) ->
	%% io:format("11 ~p~n",[E]),
	Val = get_text(E#xmlElement.content),
	%% io:format("Name:~p~n",[Val]),
	case db:get_interface(Val) of 
	     [] -> 	
	     		Str = io_lib:format("<error/>",[]),
	             	{ok, Str};
		[If1] ->  %% io:format("DB If : ~p~n", [If1]), 
			sub_node(SubNode, T, If1)
	end;

top_node(Tree, [H|T], If) when record(H, xmlElement) ->
	%% io:format("22, E~p~n",[H]),
	top_node(Tree, T, If);

top_node(Tree, [H|T], If) when record(H, xmlText) ->
	%% io:format("331  ~p~n",[H]),
	top_node(Tree, T, If);

top_node(_, [], If) ->
	%% io:format("INTERFACE SET11:~p~n", [If]),
	Str = io_lib:format("<ok/>",[]),
	{ok, Str};

top_node(Node, _E, _If) ->
	io:format("Error:~p~n",[_E]),
	Str = io_lib:format("<error/>",[]),
	{ok, Str}.

sub_node(SNodes, [E|T], If) ->
	%% io:format("4444  ~p~n",[E]),
	If2 = process_attributes(SNodes, E, If),
	%% io:format("55  ~p~n",[T]),
	sub_node(SNodes, T, If2);

sub_node(_, [], If) ->
	%% io:format("INTERFACE SET22:~p~n", [If]),
	db:set_record(If),
	Str = io_lib:format("<ok/>",[]),
	{ok, Str}.

process_attribute(ipaddress, #xmlElement{name = ipaddress}=E, If) 
   when record(E, xmlElement) ->
	Val = get_text(E#xmlElement.content),
	%% io:format("66  ~p~n",[E]),
	If1 = If#interface{ ipaddress = Val },
	{true, If1};

process_attribute(mtu, #xmlElement{name = mtu}=E, If) 
   when record(E, xmlElement) ->
	Val = get_text(E#xmlElement.content),
	%% io:format("77  Val ~p~n",[Val]),
	ValI = list_to_integer(Val),
	If1 = If#interface{ mtu = ValI },
	{true, If1};
	
process_attribute(netmask, #xmlElement{name = netmask}=E, If) 
   when record(E, xmlElement) ->
	%% io:format("88  ~p~n",[E]),
	Val = get_text(E#xmlElement.content),
	If1 = If#interface{ netmask = Val },
	{true, If1};

process_attribute(type, #xmlElement{name = type}=E, If) 
   when record(E, xmlElement) ->
	%% io:format("99  ~p~n",[E]),
	Val = get_text(E#xmlElement.content),
	If1 = If#interface{ type = Val },
	{true, If1};

process_attribute(status, #xmlElement{name = status}=E, If) 
   when record(E, xmlElement) ->
	%% io:format("111  ~p~n",[E]),
	Val = get_text(E#xmlElement.content),
	If1 = If#interface{ status = Val },
	{true, If1};
	
process_attribute(_, _, If) -> 
	%% io:format("122  ~p~n",[If]),
	{false, If}.
	
process_attributes([H|T], E, If) when record(E, xmlElement) ->
	case process_attribute(H, E, If) of
	   {true, If1} -> If1;
	   {false, _If2} -> process_attributes(T, E, If)
	end;
process_attributes(_, _, If) ->
	%% io:format("133  ~p~n",[If]),
	If.

get_text([X|_]) when record(X, xmlText) ->  X#xmlText.value;
get_text(_) ->  [].
